package com.xydtech.common.config;

import com.xydtech.common.utils.ServletUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 服务相关配置
 *
 * @author xydtech
 *
 */
@Component
public class ServerConfig {
    /**
     * 获取完整的请求路径，包括：域名，端口，上下文访问路径
     *
     * @return 服务地址
     */
    public String getUrl() {
        HttpServletRequest request = ServletUtils.getRequest();
        return getDomain(request);
    }

    public static String getDomain(HttpServletRequest request) {
        StringBuffer url = request.getRequestURL();
        String contextPath = request.getServletContext().getContextPath();
        return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString();
    }

    /**
     *富文本summerNote内容图片路径保存，ContextPath替换处理（ContextPath配置后缀使用）
     * @Title: replaceContextPath
     * @param request
     * @param summerNote
     * @return java.lang.String
     * @exception
     * @author JHON
     * @Date 2022/11/14 13:14
     **/
    public String replaceContextPath(HttpServletRequest request, String summerNote) {
        String contextPath = request.getServletContext().getContextPath();
        if (contextPath.length() > 1) {
            if (summerNote.contains(contextPath + "//")) {
                summerNote = summerNote.replaceAll("//", "/");
                summerNote = summerNote.replaceAll(contextPath, "");
            } else {
                summerNote = summerNote.replaceAll(contextPath, "");
            }
        }
        return summerNote;
    }
}
